# -*- coding: utf-8 -*-

import pyglet
import stack
import state

class GameEngine:

    def __init__(self, title="shooter", width=800, height=600):
        self.window = pyglet.window.Window(width, height)

        self.stack = stack.Stack()
        self.nextState = False
        
        pyglet.app.run

    def cleanup(self):
        while(not self.stack.empty()):
            self.stack.top().cleanup()
            self.stack.pop()
            pygame.quit()

    def changeState(self, state):
        if(not self.stack.empty()):
            self.stack.top().cleanup()
            self.stack.pop()

        self.stack.push(state)
        self.stack.top().init()

    def pushState(self, state):
        if(not self.stack.empty()):
            self.stack.top().pause()

        self.stack.push(state)
        self.stack.top().init()

    def popState(self):
        if(not self.stack.empty()):
            self.stack.top().cleanup()
            self.stack.pop()
            self.stack.top().resume()

        if(self.stack.empty()):
            self.stack.top().resume()

    def top(self):
        return self.stack.top()

    def testrun(self):
        self.stack.top().isrunning()

    def isrunning(self):
        return self.stack.top().isrunning()

    def handleevents(self):
        self.stack.top().handleevents()

    def update(self):
        self.stack.top().update()

    def draw(self):
        self.stack.top().draw()
